/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufrn.dimap.arquivo;

import br.ufrn.dimap.Grafo.Rede;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author augusto
 */
public class ArquivoDAO {
    public void salvarGrafo(Rede r, String arq){
        try {
            /*
            ObjectOutputStream objGravar = null;
            try {
                FileOutputStream arquivoGrav = new FileOutputStream(arq);
                objGravar = new ObjectOutputStream(arquivoGrav);
                objGravar.writeObject(r);
                objGravar.flush();
                objGravar.close();
                arquivoGrav.flush();
                arquivoGrav.close();
                objGravar.close();
            } catch (IOException ex) {
                Logger.getLogger(ArquivoDAO.class.getName()).log(Level.SEVERE, null, ex);
            }*/
            FileOutputStream fileStream = new FileOutputStream(arq, false);
                ObjectOutputStream os = new ObjectOutputStream(fileStream);
                os.writeObject(r);
                os.close();
        } catch (IOException ex) {
            Logger.getLogger(ArquivoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
   public Rede abrirGrafo(String arq){
        ObjectInputStream objReader = null;
        Rede r = null;
        try {
            FileInputStream arquivoReader = new FileInputStream(arq);
            objReader = new ObjectInputStream(arquivoReader);
            r = (Rede)objReader.readObject();
            objReader.close();
            arquivoReader.close();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ArquivoDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ArquivoDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            return r;
        }
       
    }
}
